using System;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Xml.Serialization;
using AFI.Speargun.Web.Helpers;
using AFI.Speargun.Web.Models;
using AFI.Speargun.Web.Models.Validation;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using AFI.Speargun.Web.Core;
using AFI.Speargun.Web.Paging;

namespace AFI.Speargun.Web.Controllers
{
    public class CategoryController : BaseController
    {
        //
        // GET: /Category/AjaxList

        [Authorize]
        public ActionResult AjaxList()
        {
            this.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            Category root = _service.GetCategory(Guid.Empty);
            string json = root.ToJson<Category>();

            return Content(json,
            "text/x-json",
            System.Text.Encoding.UTF8);
        }

        //
        // GET: /Category/

        [Authorize]
        public ActionResult Index(Guid? id, int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;

            Category category = _service.GetCategory(id);

            ViewData["Appellation"] = category.Appellation;
            ViewData["CategoryId"] = category.CategoryId;

            if (!category.AssetEntry.IsLoaded)
                category.AssetEntry.Load();

            var assetEntries = category.AssetEntry.ToPagedList(currentPageIndex, pageSize);

            return View(assetEntries);
        }

        //
        // GET: /Category/Create/{id}

        [Authorize]
        public ActionResult Create(Guid id)
        {
            return View(_service.GetCategory(id));
        }

        //
        // POST: /Category/Create/{id}

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]
        public ActionResult Create(Guid id, [Bind(Exclude = "CategoryId")] Category category)
        {
            if (_service.CreateCategory(id, category))
                return RedirectToAction("Index", new { id = category.CategoryId });
            return View();
        }

        //
        // POST: /Category/AjaxCreate

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]
        public ActionResult AjaxCreate(Guid parentCategoryId)
        {
            int returnValue = 0;
            string returnMessage = String.Empty;

            Category category = new Category()
            {
                Appellation = "New Category",
            };

            if (!_service.CreateCategory(parentCategoryId, category))
            {
                returnValue = 1;
                returnMessage = ModelState.GetValidationSummary();
            }

            JObject returnJson =
                new JObject(
                    new JProperty("returnValue", returnValue),
                    new JProperty("returnMessage", returnMessage)
                );

            return Content(returnJson.ToString(),
                "text/x-json",
                System.Text.Encoding.UTF8);
        }

        //
        // GET: /Category/Edit/{id}

        [Authorize]
        public ActionResult Edit(Guid id)
        {
            return View(_service.GetCategory(id));
        }

        //
        // POST: /Category/Edit/{id}

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]
        public ActionResult Edit(Guid id, string Appellation)
        {
            var category = _service.GetCategory(id);
            category.Appellation = Appellation;

            if (_service.EditCategory(category))
                return RedirectToAction("Index", new { id = category.CategoryId });
            return View(category);
        }

        //
        // GET: /Category/EditAssetEntryAdditionalDefinition/{id}

        [Authorize]
        public ActionResult EditAssetEntryAdditionalDefinition(Guid id)
        {
            return View(_service.GetCategory(id));
        }

        //
        // POST: /Category/EditAssetEntryAdditionalDefinition/{id}

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]
        public ActionResult EditAssetEntryAdditionalDefinition(Guid id, FormCollection collection)
        {
            Category category = _service.GetCategory(id);
            category.AssetEntryAdditionalDefinitionSchema = new AssetEntryAdditionalDefinitionSchema(collection);

            if (_service.EditCategory(category))
                return RedirectToAction("Index", new { id = id });
            return View(category);
        }

        //
        // POST: /Category/AjaxRename

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]
        public ActionResult AjaxRename(Guid categoryId, string appellation)
        {
            int returnValue = 0;
            string returnMessage = String.Empty;

            var category = _service.GetCategory(categoryId);
            category.Appellation = appellation;

            if (!_service.EditCategory(category))
            {
                returnValue = 1;
                returnMessage = ModelState.GetValidationSummary();
            }

            JObject returnJson =
                new JObject(
                    new JProperty("returnValue", returnValue),
                    new JProperty("returnMessage", returnMessage)
                );

            return Content(returnJson.ToString(),
                "text/x-json",
                System.Text.Encoding.UTF8);
        }

        //
        // GET: /Categroy/Delete/{id}

        [Authorize]
        public ActionResult Delete(Guid id)
        {
            var category = _service.GetCategory(id);
            return View(category);
        }

        //
        // POST: /Category/Delete/{id}

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]
        public ActionResult Delete(Guid id, Category category)
        {
            if (_service.DeleteCategory(id))
                return RedirectToAction("Index");
            return View(category);
        }

        //
        // POST: /Category/AjaxDelete

        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize]
        public ActionResult AjaxDelete(Guid categoryId)
        {
            int returnValue = 0;
            string returnMessage = String.Empty;

            if (!_service.DeleteCategory(categoryId))
            {
                returnValue = 1;
                returnMessage = ModelState.GetValidationSummary();
            }

            JObject returnJson =
                new JObject(
                    new JProperty("returnValue", returnValue),
                    new JProperty("returnMessage", returnMessage)
                );

            return Content(returnJson.ToString(),
                "text/x-json",
                System.Text.Encoding.UTF8);
        }
    }
}
